package top.shiyiri.datetime;

import org.junit.Test;

import java.util.Calendar;
import java.util.Date;

/**
 * @author Aunean
 * @date 2022/1/3
 */
/*
jdk8 之前的日期时间的API测试
Calendar 日历类（抽象类）的使用
 */
public class CalendarTest {

    @Test
    public void calendarTest1() {
        //1.实例化
        //方式一：创建其子类（GregorianCalendar）的对象
        //方式二：调用其静态方法 getInstance()
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);//java.util.GregorianCalendar[time=1641198572341,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=0,WEEK_OF_YEAR=2,WEEK_OF_MONTH=2,DAY_OF_MONTH=3,DAY_OF_YEAR=3,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=29,SECOND=32,MILLISECOND=341,ZONE_OFFSET=28800000,DST_OFFSET=0]
        System.out.println(calendar.getClass());//class java.util.GregorianCalendar

        //2.常用方法
        //get()
        //DAY_OF_MONTH 本月的第几天
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        //DAY_OF_WEEK 本周的第几天
        int week_day = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println(week_day);

        //set()
        //设置今天为本月的第22天
        calendar.set(Calendar.DAY_OF_MONTH, 22);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //add()
        calendar.add(Calendar.DAY_OF_MONTH, -3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //getTime()：日历类 -> Date
        Date date = calendar.getTime();
        System.out.println(date);

        //setTime()：Date -> 日历类
        Date date1 = new Date();
        calendar.setTime(date1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }


}
